package com.tanhua.recommend.jobs;

import com.tanhua.domain.mongo.Movement;

import com.tanhua.domain.mongo.RecommendMovement;

import com.tanhua.domain.mongo.RecommendVideo;

import com.tanhua.domain.mongo.Video;

import org.apache.commons.lang3.RandomUtils;

import org.bson.types.ObjectId;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.query.Criteria;

import org.springframework.data.mongodb.core.query.Query;

import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

import java.util.Date;

import java.util.Set;

@Component

public class RedisToMongoJob {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Scheduled(cron = "0 * * * * ?")
    public void movementRedisToMongoJob(){

//        要把redis中的数据放到mongo中

//        把给所有人推荐的数据放到mongo中

//        QUANZI_PUBLISH_RECOMMEND_*  100个人

        Set<String> keys = redisTemplate.keys("QUANZI_PUBLISH_RECOMMEND_*");
        for (String key : keys) {  //key  QUANZI_PUBLISH_RECOMMEND_1

//            获取这个人推荐的圈子id

            String userIdStr = key.replace("QUANZI_PUBLISH_RECOMMEND_","");
            long userId = Long.parseLong(userIdStr);

//            再放入mongo数据之前应该根据用户删除原来的数据

            mongoTemplate.remove(new Query(Criteria.where("userId").is(userId)),RecommendMovement.class);



            String pidString = redisTemplate.boundValueOps(key).get(); //21,26,19,1461550774898266112,20,100099,28,10064,20,10020
            String[] pids = pidString.split(",");

            for (String pid : pids) {
                Query query = new Query(Criteria.where("pid").is(Long.parseLong(pid)));
                Movement movement = mongoTemplate.findOne(query, Movement.class);
                if(movement==null){
                    continue;
                }
                RecommendMovement recommendMovement = new RecommendMovement();
                recommendMovement.setId(new ObjectId());
                recommendMovement.setUserId(userId);
                recommendMovement.setPid(Long.parseLong(pid));

//                需要根据pid查询movementId

                recommendMovement.setMovementId(movement.getId()); //
                recommendMovement.setScore(RandomUtils.nextDouble(80,90)); //
                recommendMovement.setCreated(new Date().getTime());
                mongoTemplate.save(recommendMovement);
            }
        }

//        每个人保存10次   21,26,19,1461550774898266112,20,100099,28,10064,20,10020

    }



    @Scheduled(cron = "0 * * * * ?")
    public void videoRedisToMongoJob(){

//        要把redis中的数据放到mongo中

//        把给所有人推荐的数据放到mongo中

//        QUANZI_PUBLISH_RECOMMEND_*  100个人

        Set<String> keys = redisTemplate.keys("QUANZI_VIDEO_RECOMMEND_*");
        for (String key : keys) {
            String userIdStr = key.replace("QUANZI_VIDEO_RECOMMEND_","");
            long userId = Long.parseLong(userIdStr);
            mongoTemplate.remove(new Query(Criteria.where("userId").is(userId)),RecommendMovement.class);

            String pidString = redisTemplate.boundValueOps(key).get(); //21,26,19,1461550774898266112,20,100099,28,10064,20,10020
            String[] pids = pidString.split(",");

            for (String pid : pids) {
                Query query = new Query(Criteria.where("vid").is(Long.parseLong(pid)));
                Video video = mongoTemplate.findOne(query, Video.class);
                if(video==null){
                    continue;
                }
                RecommendVideo recommendVideo = new RecommendVideo();
                recommendVideo.setId(new ObjectId());
                recommendVideo.setUserId(userId);
                recommendVideo.setVideoId(Long.parseLong(pid));

//                需要根据pid查询movementId

                recommendVideo.setMovementId(video.getId()); //
                recommendVideo.setScore(RandomUtils.nextDouble(80,90)); //
                recommendVideo.setCreated(new Date().getTime());
                mongoTemplate.save(recommendVideo);
            }
        }

//        每个人保存10次   21,26,19,1461550774898266112,20,100099,28,10064,20,10020

    }

}
